package com.drama.api.company.bank.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.drama.api.base.*;
import com.drama.api.company.bank.domain.vo.*;
import com.drama.api.company.bank.service.ICompanyBankService;
import com.drama.util.BindingResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api( tags = ApiIndex.CompanyBankController)
@RequestMapping(value = "/api/company/bank")
@RestController
@SaCheckLogin
public class CompanyBankController {

    @Autowired
    private ICompanyBankService service;

    @ApiOperation(value = "查询商户银行卡列表")
    @GetMapping(value = "/list")
    public R<RestPage<List<CompanyBankVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute CompanyBankSearchVO searchVO) {
        List<CompanyBankVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "创建商户银行卡")
    @PostMapping(value = "/add")
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody CompanyBankAddVO vo, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.add(vo);
        }
        return result;
    }

    @ApiOperation(value = "删除商户银行卡")
    @PostMapping(value = "/delete")
    public R delete( @ApiParam(value = "修改参数") @Valid @RequestBody IDEntity delete) {
        service.delete(delete.getId());
        return new R();
    }


    @ApiOperation(value = "修改商户银行卡")
    @PostMapping(value = "/update")
    public R update(@ApiParam(value = "修改参数") @Valid  @RequestBody CompanyBankUpdateVO updateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.update(updateVO);
        }
        return result;
    }

    @ApiOperation(value = "查询默认银行卡信息")
    @GetMapping(value = "/get_default")
    public R<CompanyBankVO> get_default() {
        R result=new R();
        result.setData(service.get_default());
        return result;
    }

    @ApiOperation(value = "设置默认银行卡信息")
    @PostMapping(value = "/set_default")
    public R set_default(@ApiParam(value = "修改参数") @Valid @RequestBody CompanyBankStateVO companyBankStateVO) {
        service.set_default(companyBankStateVO.getId());
        return new R();
    }

}
